package syswk.com.tanksgame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * 作者：swk
 * 时间：
 * 公司：
 **/
@SuppressWarnings({"all"})
public class MyPanel extends JPanel implements KeyListener, Runnable {
    public Hero hero = null;
    Vector<EnemyTank> enemyTanks = new Vector<>();

    Vector<Bomb> bombs = new Vector<>();
    public Image image1 = null;
    public Image image2 = null;
    public Image image3 = null;

    /**
     * 初始化坦克
     */
    public MyPanel() {
        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/1.png"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/2.png"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/3.png"));
        this.setBackground(Color.black);
        hero = new Hero(100, 600, 0, true, Color.CYAN);
        hero.setSpeed(10);
        for (int i = 0; i < tankGames.ENEMY_TANKS_NUM; i++) {
            EnemyTank enemyTank = new EnemyTank((100 * (i + 1)), 0, 2, true, Color.yellow);
            Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect(), true, tankGames.ENEMY_SHOT_SPEED);
            enemyTank.shots.add(shot);
            enemyTanks.add(enemyTank);
            new Thread(shot).start();
            new Thread(enemyTank).start();
        }
    }

    /**
     * 绘制坦克
     *
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, tankGames.WIDTH, tankGames.HEIGHT);
        //绘制我方坦克
        if (hero != null && hero.getLift()) {
            DrawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0);
            for (int i = 0; i < hero.shots.size(); i++) {
                Shot shot = hero.shots.get(i);
                if (shot != null && shot.getLift() == true) {
                    g.draw3DRect(shot.getX(), shot.getY(), 2, 2, false);
                } else {
                    hero.shots.remove(shot);
                }
            }
        }


        //绘制敌方坦克
        for (int j = 0; j < enemyTanks.size(); j++) {
            EnemyTank enemyTank = enemyTanks.get(j);
            enemyTank.setSpeed(6);
            if (enemyTank.getLift()) {
                DrawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 1);
                for (int i = 0; i < enemyTank.shots.size(); i++) {
                    Shot shot = enemyTank.shots.get(i);
                    if (shot != null && shot.getLift() == true) {
                        g.draw3DRect(shot.getX(), shot.getY(), 2, 2, false);
                    } else {
                        enemyTank.shots.remove(shot);
                    }
                }
            }
        }

        //绘制炸弹
        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            if (bomb.life > 6) {
                while (!g.drawImage(image1, bomb.x, bomb.y, 60, 60, this)){
                    if(g.drawImage(image1, bomb.x, bomb.y, 60, 60, this)){
                        break;
                    }
                }
            } else if (bomb.life > 3) {
                while (!g.drawImage(image2, bomb.x, bomb.y, 60, 60, this)){
                    if(g.drawImage(image2, bomb.x, bomb.y, 60, 60, this)){
                        break;
                    }
                }
            } else {
                while (!g.drawImage(image3, bomb.x, bomb.y, 60, 60, this)){
                    if(g.drawImage(image3, bomb.x, bomb.y, 60, 60, this)){
                        break;
                    }
                }
            }
            bomb.lifeDown();
            if (bomb.life == 0) {
                bombs.remove(bomb);
            }
        }

    }

    public void DrawTank(int x, int y, Graphics g, int direct, int type) {
        switch (type) {
            case 0:
                g.setColor(Color.CYAN);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        switch (direct) { //坦克方向
            case 0:
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y, x + 20, y + 30);
                break;
            case 1:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 60, y + 20, x + 30, y + 20);
                break;
            case 2:
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 60, x + 20, y + 30);
                break;
            case 3:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x, y + 20, x + 30, y + 20);
                break;


        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //System.out.println("触发事件，我方坦克坐标：X:" + hero.getX() + "  Y:" + hero.getY());
        if (e.getKeyCode() == KeyEvent.VK_W) {
            hero.setDirect(0);
            if (hero.getY() > 0) {
                hero.moveUp();
            }
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            hero.setDirect(1);
            if (hero.getX() + 80 < tankGames.WIDTH) {
                hero.moveRight();
            }
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.setDirect(2);
            if (hero.getY() + 100 < tankGames.HEIGHT) {
                hero.moveDown();
            }
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.setDirect(3);
            if (hero.getX() > 0) {
                hero.moveLeft();
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            hero.shotEnemyTank();
//            if (hero.shot == null || !hero.shot.getLift()) {
//                hero.shotEnemyTank();
//            }
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            hitEnemyTank();
            myDead();
            this.repaint();
        }
    }

    public void myDead() {
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            for (int j = 0; j < enemyTank.shots.size(); j++) {
                Shot shot = enemyTank.shots.get(j);
                if (shot != null && shot.getLift()) {
                    switch (hero.getDirect()) {
                        case 0:
                        case 2:
                            if (shot.getX() > hero.getX() && shot.getX() < (hero.getX() + 40)
                                    && shot.getY() > hero.getY() && shot.getY() < (hero.getY() + 60)) {
                                shot.setLift(false);
                                hero.setLift(false);
                                Bomb bomb = new Bomb(hero.getX(), hero.getY());
                                bombs.add(bomb);
                            }

                            break;
                        case 1:
                        case 3:
                            if (shot.getX() > hero.getX() && shot.getX() < (hero.getX() + 60)
                                    && shot.getY() > hero.getY() && shot.getY() < (hero.getY() + 40)) {
                                shot.setLift(false);
                                hero.setLift(false);
                                Bomb bomb = new Bomb(hero.getX(), hero.getY());
                                bombs.add(bomb);
                            }

                            break;
                    }
                }

            }

        }
    }

    public void hitEnemyTank() {
        for (int j = 0; j < hero.shots.size(); j++) {
            Shot shot = hero.shots.get(j);
            if (shot != null && shot.getLift()) {
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    hitTank(shot, enemyTank);
                }
            }
        }

    }

    public void hitTank(Shot shot, Tank tank) {
        switch (tank.getDirect()) {
            case 0:
            case 2:
                if (shot.getX() > tank.getX() && shot.getX() < (tank.getX() + 40)
                        && shot.getY() > tank.getY() && shot.getY() < (tank.getY() + 60)) {
                    Bomb bomb = new Bomb(tank.getX(), tank.getY());
                    bombs.add(bomb);
                    shot.setLift(false);
                    tank.setLift(false);
                    enemyTanks.remove(tank);
                }
                break;
            case 1:
            case 3:
                if (shot.getX() > tank.getX() && shot.getX() < (tank.getX() + 60)
                        && shot.getY() > tank.getY() && shot.getY() < (tank.getY() + 40)) {
                    Bomb bomb = new Bomb(tank.getX(), tank.getY());
                    bombs.add(bomb);
                    shot.setLift(false);
                    tank.setLift(false);
                    enemyTanks.remove(tank);
                }
                break;
        }
    }
}
